/*
 * This file is part of the 京墨（jingmo）APP.
 *
 * (c) 贺丰宝（hefengbao） <hefengbao@foxmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

package com.hefengbao.jingmo.ui.screen.chinese.tonguetwister

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.hefengbao.jingmo.R
import com.hefengbao.jingmo.data.database.entity.chinese.TongueTwisterEntity
import com.hefengbao.jingmo.ui.component.SimpleScaffold

@Composable
fun TongueTwisterIndexRoute(
    viewModel: TongueTwisterIndexViewModel = hiltViewModel(),
    onBackClick: () -> Unit,
    onItemClick: (Int) -> Unit
) {
    val tongueTwisterEntities by viewModel.tongueTwisterEntities.collectAsState(initial = emptyList())

    TongueTwisterIndexScreen(
        onBackClick = onBackClick,
        tongueTwisterEntities = tongueTwisterEntities,
        onItemClick = onItemClick
    )
}

@Composable
private fun TongueTwisterIndexScreen(
    modifier: Modifier = Modifier,
    onBackClick: () -> Unit,
    tongueTwisterEntities: List<TongueTwisterEntity>,
    onItemClick: (Int) -> Unit
) {
    SimpleScaffold(
        onBackClick = onBackClick,
        title = stringResource(R.string.chinese_tonguetwister)
    ) {
        LazyColumn(
            modifier = modifier.fillMaxWidth()
        ) {
            itemsIndexed(
                items = tongueTwisterEntities,
                key = { _: Int, item: TongueTwisterEntity -> item.id }
            ) { _: Int, item: TongueTwisterEntity ->
                Text(
                    text = item.title,
                    modifier = modifier
                        .fillMaxWidth()
                        .clickable {
                            onItemClick(item.id)
                        }
                        .padding(16.dp)
                )
            }
        }
    }
}